{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter                   # Counter 可用来统计可迭代对象中，元素的数量\n",
    "f = open('从百草园到三味书屋.txt', encoding='utf8')  # encoding 要使用和这个文件对应的编码\n",
    "txt = f.read()                                    # 读取全部字符\n",
    "f.close()\n",
    "\n",
    "cnt = Counter(txt)  # 得到每个字符的出现次数\n",
    "char_list = []      # 定义空的列表\n",
    "for char in cnt:\n",
    "    if char in \"\\u3000\\n 。,:!！“?…”《》，；—（）-：？^~`[]|\":  # 过滤掉常见的标点和空白字符\n",
    "        continue\n",
    "    char_list.append([cnt[char], char])  # 把字符和字符出现的次数加入列表\n",
    "\n",
    "char_list.sort(reverse=True)  # 降序排序\n",
    "# 以下代码打印前100的字符和出现次数\n",
    "for char in char_list:\n",
    "    print(char[0], char[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiwAAAGdCAYAAAAxCSikAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAb3klEQVR4nO3df3SWdf348dfGYAxlQzA2yE1WcULTCiFx4KnPyZ2oOKVJP+xQB82jZbMcdDKooGOFQysjzSA9RXbSSM9RSys7nlmUpwmIP4pMtKMGR9uobLv9EcPY+/tHp/vLhI4MN+532+Nxzn0Ou65r1/3e/fa457nu932tLKWUAgAgY+WlHgAAwEsRLABA9gQLAJA9wQIAZE+wAADZEywAQPYECwCQPcECAGSvotQDeLG+vr546qmnYvz48VFWVlbq4QAAByGlFM8880xMnTo1yssH/3pIdsHy1FNPRX19famHAQAcgp07d8Yxxxwz6OfNLljGjx8fERGvvOB7UV45LrZdMr/EIwIAXkqhUIj6+vri7/HBll2w/OdtoPLKcV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SERElJWVRWtra3z5y1+O6dOnR2NjY6xYsSKmTp0aZ5xxxlCMHwAYAQYULFdddVWsWLEiPv7xj8euXbti6tSp8dGPfjRWrlxZPObiiy+O5557Ls4///zo7u6OU089Ne64444YO3bsoA8eABgZytK+t6nNQKFQ+Pfi29Ybo7xyXDyxekGphwQAvIT//P7u6ekZkuUd/pYQAJA9wQIAZE+wAADZEywAQPYECwCQPcECAGRPsAAA2RMsAED2BAsAkD3BAgBkT7AAANkTLABA9gQLAJA9wQIAZE+wAADZEywAQPYECwCQPcECAGRPsAAA2RMsAED2BAsAkD3BAgBkT7AAANkTLABA9gQLAJA9wQIAZE+wAADZEywAQPYECwCQPcECAGRPsAAA2RMsAED2BAsAkD3BAgBkT7AAANkTLABA9gQLAJA9wQIAZE+wAADZEywAQPYECwCQPcECAGRPsAAA2RMsAED2BAsAkD3BAgBkT7AAANkTLABA9gQLAJA9wQIAZE+wAADZEywAQPYECwCQPcECAGRPsAAA2RMsAED2BAsAkD3BAgBkT7AAANkTLABA9gQLAJA9wQIAZE+wAADZEywAQPYECwCQvQEHy5NPPhkf+tCHYtKkSVFVVRUnnnhi3HvvvcX9KaVYuXJlTJkyJaqqqqK5uTkeffTRQR00ADCyDChY/vGPf8S8efNi9OjR8fOf/zweeuih+NrXvhZHHXVU8ZjLL788rrzyyli3bl1s2rQpjjjiiJg/f37s3r170AcPAIwMFQM5+LLLLov6+vpYv359cVtjY2Px3ymlWLNmTXz+85+P008/PSIivv/970dtbW3ceuutcdZZZw3SsAGAkWRAV1h+8pOfxOzZs+N973tfTJ48OWbOnBnXXnttcf/jjz8enZ2d0dzcXNxWU1MTc+bMiY6OjgOes7e3NwqFQr8HAMC+BhQsjz32WKxduzamT58ev/jFL+KCCy6IT37yk3HddddFRERnZ2dERNTW1vb7vtra2uK+F2tra4uamprio76+/lB+DgBgGBtQsPT19cVJJ50Ul156acycOTPOP//8OO+882LdunWHPIDly5dHT09P8bFz585DPhcAMDwNKFimTJkSxx9/fL9txx13X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miy66KO68884YO3ZsqYczovX19cXs2bPj0ksvjYiImTNnxrZt22LdunWxePHiEo9uZLnxxhvj+uuvjxtuuCFe97rXxQMPPBCtra0xdepUczGCZPOW0NFHHx2jRo3a79MOXV1dUVdXV6JRjSwXXnhh3H777fHLX/4yjjnmmOL2urq62LNnT3R3d/c73twMvq1bt8auXbvipJNOioqKiqioqIiNGzfGlVdeGRUVFVFbW2suDpMpU6bE8ccf32/bcccdFzt27IiIKL7e/p819D796U/HsmXL4qyzzooTTzwxPvzhD8eSJUuira0tIsxFKR3Ma19XVxe7du3qt/9f//pXPP300wOan2yCZcyYMTFr1qxob28vbuvr64v29vZoamoq4ciGv5RSXHjhhXHLLbfEXXfdFY2Njf32z5o1K0aPHt1vbrZv3x47duwwN4PstNNOi9///vfxwAMPFB+zZ8+ORYsWFf9tLg6PefPm7ffx/kceeSSOPfbYiIhobGyMurq6fnNRKBRi06ZN5mKQPf/881Fe3v/X1ahRo6Kvry8izEUpHcxr39TUFN3d3bF169biMXfddVf09fXFnDlzDv7JXvaS4UG0YcOGVFlZmb73ve+lhx56KJ1//vlpwoQJqbOzs9RDG9YuuOCCVFNTk371q1+lv/zlL8XH888/XzzmYx/7WGpoaEh33XVXuvfee1NTU1Nqamoq4ahHjn0/JZSSuThcNm/enCoqKtKqVavSo48+mq6//vo0bty49IMf/KB4zOrVq9OECRPSj3/84/S73/0unX766T5KOwQWL16cXvnKVxY/1nzzzTeno48+Ol188cXFY8zF0HnmmWfS/fffn+6///4UEemKK65I999/f/rzn/+cUjq41/7tb397mjlzZtq0aVO6++670/Tp0/+3P9acUkpXXXVVamhoSGPGjEknn3xyuueee0o9pGEvIg74WL9+ffGYf/7zn+njH/94Ouqoo9K4cePSe97znvSXv/yldIMeQV4cLObi8LntttvSCSeckCorK9OMGTPSNddc029/X19fWrFiRaqtrU2VlZXptNNOS9u3by/RaIevQqGQLrrootTQ0JDGjh2bXvWqV6XPfe5zqbe3t3iMuRg6v/zlLw/4O2Lx4sUppYN77f/+97+nD37wg+nII49M1dXV6ZxzzknPPPPMgMZRltI+twoEAMhQNmtYAAD+G8ECAGRPsAAA2RMsAED2BAsAkD3BAgBkT7AAANkTLABA9gQLAJA9wQIAZE+wAADZEywAQPb+H8Qf4ioucWoLAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 以下代码使用 matplotlib 库绘制出现次数前100的字符出现次数分布图\n",
    "from matplotlib import pyplot as plt\n",
    "x = []\n",
    "y = []\n",
    "for i, char_cnt in enumerate(char_list):\n",
    "    x.append(i)\n",
    "    y.append(char_cnt[0])\n",
    "plt.axis((0,100, 0, max(y)))\n",
    "plt.bar(x[:100], y[:100], width=1)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
